package com.huaqin.excel.handler;

public class ExcelAppInfoHandler {

	ExcelReadAndWrite mGmsPackageInfo = new ExcelReadAndWrite();

	// get Gms appInfo from source excel
	public StringBuffer readGmsPackageInfo(boolean isInternal, String Template,
			int sheetX, int checklistRow) {

		StringBuffer appsInfo = new StringBuffer();
		mGmsPackageInfo.loadTemplate(Template, sheetX);
		String appName = null;
		String appVersion = null;
		String appInfo = null;
		// according checklistRow to get relative appInfo
		if (!isInternal) {
			switch (checklistRow) {
			case ExcelConstants.GMS_APP_ALL:
				System.out.println("write all gms app info...");
				// get all GmsAppInfo
				for (int i = 3; i < 40; i++) {

					appName = mGmsPackageInfo.readInCell(i, 0);
					appVersion = mGmsPackageInfo.readInCell(i, 5);
					appInfo = appName + " " + appVersion + "   " + "\r\n";
					if (!appName.isEmpty() && !appVersion.isEmpty()) {
						appsInfo.append(appInfo);
					}

				}
//				System.out.println("**GMS_APP_ALL**" + appsInfo.toString()
//						+ "******");
				break;

			case ExcelConstants.GMS_APP_PRE:
				// get pre GmsAppInfo
				System.out.println("write pre gms app info...");
				for (int i = 2; i < 40; i++) {
					appName = mGmsPackageInfo.readInCell(i, 0);
					appVersion = mGmsPackageInfo.readInCell(i, 5);

					if (ExcelConstants.TAG_APP_PRE.equals(appName)) {
						i = i + 1;
						for (int j = i; j < 40; j++) {

							appName = null;
							appVersion = null;

							appName = mGmsPackageInfo.readInCell(j, 0);
							if (ExcelConstants.TAG_APP_FRA.equals(appName)) {
								break;
							}
							appVersion = mGmsPackageInfo.readInCell(j, 5);
							appInfo = appName + " " + appVersion + "   "
									+ "\r\n";
							if (!appName.isEmpty() && !appVersion.isEmpty()) {
								appsInfo.append(appInfo);
							}

						}
						break;

					}

				}
//				System.out.println("***GMS_APP_PRE**" + appsInfo.toString()
//						+ "******");
				break;
			case ExcelConstants.GMS_APP_FRA:
				System.out.println("write fra gms app info...");
				// get pre GmsAppInfo
				for (int i = 3; i < 40; i++) {
					appName = mGmsPackageInfo.readInCell(i, 0);
					appVersion = mGmsPackageInfo.readInCell(i, 5);
					if (ExcelConstants.TAG_APP_FRA.equals(appName)) {
						i = i + 1;
						for (int j = i; j < 40; j++) {
							appName = null;
							appVersion = null;
							appName = mGmsPackageInfo.readInCell(j, 0);
							if (ExcelConstants.TAG_APP_SYS.equals(appName)) {
								break;
							}
							appInfo = appName + " " + "\r\n";

							appsInfo.append(appInfo);
						}
						break;

					}

				}
//				System.out.println("***GMS_APP_FRA**" + appsInfo.toString()
//						+ "******");
				break;
			case ExcelConstants.GMS_APP_SYS:
				System.out.println("write sys gms app info...");
				// get pre GmsAppInfo
				for (int i = 3; i < 40; i++) {
					appName = mGmsPackageInfo.readInCell(i, 0);
					appVersion = mGmsPackageInfo.readInCell(i, 5);

					if (ExcelConstants.TAG_APP_SYS.equals(appName)) {
						i = i + 1;
						for (int j = 3; j < 20; j++) {
							appName = null;
							appVersion = null;
							appInfo = null;

							appName = mGmsPackageInfo.readInCell(i, 0);
							appVersion = mGmsPackageInfo.readInCell(i, 5);
							appInfo = appName + " " + appVersion + "   "
									+ "\r\n";
							if (!appName.isEmpty() && !appVersion.isEmpty()) {
								appsInfo.append(appInfo);
							}
							i = i + 1;
//							System.out.println(i);
						}

					}

				}
//				System.out.println("***GMS_APP_SYS**" + appsInfo.toString()
//						+ "******");
				break;

			}
		} else {
			// Internal Version
			if (ExcelConstants.GMS_APP_CHN == checklistRow) {
				appName = mGmsPackageInfo.readInCell(30, 0);
				appVersion = mGmsPackageInfo.readInCell(30, 5);
				appInfo = appName + " " + appVersion + "   " + "\r\n";
				if (!appName.isEmpty() && !appVersion.isEmpty()) {
					appsInfo.append(appInfo);
				}

				appName = mGmsPackageInfo.readInCell(9, 0);
				appVersion = mGmsPackageInfo.readInCell(9, 5);
				appInfo = appName + " " + appVersion + "   " + "\r\n";
				if (!appName.isEmpty() && !appVersion.isEmpty()) {
					appsInfo.append(appInfo);
				}
//				System.out.println("***GMS_APP_CHN**" + appsInfo.toString()
//						+ "******");

			} else {
				appsInfo = new StringBuffer("NA");
//				System.out.println("***GMS_APP_CHN_NA**" + appsInfo.toString()
//						+ "******");
			}
		}

		return appsInfo;
	}
}